package com.example.es.utils;

import com.example.es.bean.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 解析网页
 * @author: Leo
 * @createDate: 2020/4/15
 * @version: 1.0
 */
public class ParseHtmlUtil
{
    public static void main(String[] args) throws Exception
    {
        parseJD("java").forEach(System.out::println);
    }

    public static List<Content> parseJD(String keywords) throws Exception
    {
        String url = "https://search.jd.com/Search?keyword=" + keywords + "&enc=utf-8&wq=java&pvid=3249537e27b14ffcaf7770d57e038e0f";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements li = element.getElementsByTag("li");
        ArrayList<Content> contents = new ArrayList<>();
        for (Element lii : li)
        {
            String img = lii.getElementsByTag("img").eq(0).attr("src");
            String price = lii.getElementsByClass("p-price").eq(0).text();
            String title = lii.getElementsByClass("p-name").eq(0).text();
            Content content = new Content();
            content.setImg(img);
            content.setTitle(title);
            content.setPrice(price);
            contents.add(content);
        }
        return contents;
    }
}
